/*
 * @Date: 2021-09-10 10:37:30
 * @Author: Acckno1
 * @LastEditTime: 2021-09-10 11:07:01
 * @Description: 前序中序建树 爬山法求LCA O(nm) 能够卡常数过掉 但是acwing上最后一个实例过不了
 */
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 1e4 + 10;

int n, m;
unordered_map<int, int> pos, parent, level;
unordered_set<int> st;
int pre[N], in[N];

int build(int u, int s, int e, int le) {
    if (s > e) return -INF;
    int root = pre[u];
    int i = pos[pre[u]];
    int left = -INF, right = -INF;
    if (i > s) left = build(u + 1, s, i - 1, le + 1);
    if (i < e) right = build(u + 1 + (i - s), i + 1, e, le + 1);

    parent[left] = root;
    parent[right] = root;
    parent[root] = -INF;
    level[root] = le;
    return root;
}

int query(int u, int v) {

    while (level[u] > level[v]) u = parent[u]; 
    while (level[v] > level[u]) v = parent[v];
    while (v != u) {
        v = parent[v];
        u = parent[u];
    }
    return v;
}

inline void solution() {
    cin >> n >> m;
    for (int i = 0; i < m; i ++ ) {
        cin >> in[i];
        pos[in[i]] = i;
        st.insert(in[i]);
    }
   
    for (int i = 0; i < m; i ++ )
        cin >> pre[i];
    
    int root = build(0, 0, m - 1, 1);
    

    for (int i = 0; i < n; i ++ ) {
        int u, v;
        cin >> u >> v;
        bool fu = false, fv = false;
        if (st.count(u)) fu = true;
        if (st.count(v)) fv = true;

        if (!fu && !fv) {
            cout << "ERROR: " << u << " and " << v << " are not found." << endl;
        } else if (fu && !fv) {
            cout << "ERROR: " << v << " is not found." << endl;
        } else if (!fu && fv) {
            cout << "ERROR: " << u << " is not found." << endl;
        } else {
            int res = query(u, v);
            if (res == u) cout << u << " is an ancestor of " << v << "." << endl;
            else if (res == v) cout << v << " is an ancestor of " << u << "." << endl;
            else cout << "LCA of " << u << " and "<< v << " is " << res << "." << endl;
        }
    }
    
}

int main() {
//     freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}